package com.gali.mall.controller;

import com.gali.mall.common.ResultInfo;
import com.gali.mall.exception.GaLiExceptionEnum;
import com.gali.mall.filiter.UserFilter;
import com.gali.mall.model.pojo.Cart;
import com.gali.mall.model.vo.CartVO;
import com.gali.mall.request.cartReq.AddCartReq;
import com.gali.mall.request.cartReq.DeleteCartReq;
import com.gali.mall.request.cartReq.SelectCartReq;
import com.gali.mall.request.cartReq.UpdateCartReq;
import com.gali.mall.service.CartService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author gali
 * @date 2022/4/21 20:07
 */
@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 添加商品至购物车
     * @param addCartReq
     * @return
     */
    @PostMapping("/cart/add")
    public ResultInfo addCart(@RequestBody @Valid AddCartReq addCartReq){
        Cart cart = new Cart();
        BeanUtils.copyProperties(addCartReq,cart);
        cart.setUserId(UserFilter.currentUser.getId());
        List<CartVO> cartVOS = cartService.addCart(cart);
        return ResultInfo.success(cartVOS);
    }

    /**
     * 查询用户全部的购物车
     * @return
     */
    @GetMapping("/cart/list")
    public ResultInfo findList(){
        Cart cart = new Cart();
        List<CartVO> list = cartService.list(UserFilter.currentUser.getId());
        if (list.size()>0){
            return ResultInfo.success(list);
        }
        return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
    }

    /**
     * 删除购物车中的某个商品
     * @param deleteCartReq
     * @return
     */
    @PostMapping("/cart/delete")
    public ResultInfo deleteCart(@RequestBody @Valid DeleteCartReq deleteCartReq){
        Cart cart = new Cart();
        BeanUtils.copyProperties(deleteCartReq,cart);
        boolean b = cartService.removeCart(cart.getId());
        if (b){
            return ResultInfo.success();
        }
        return ResultInfo.error(GaLiExceptionEnum.DELETE_ERROR);
    }

    /**
     * 更新购物车的商品
     * @param updateCartReq
     * @return
     */
    @PostMapping("/cart/update")
    public ResultInfo updateCart(@RequestBody @Valid UpdateCartReq updateCartReq){
        Cart cart = new Cart();
        BeanUtils.copyProperties(updateCartReq,cart);
        boolean b = cartService.reviseCart(cart);
        if (b){
            return ResultInfo.success();
        }
        return ResultInfo.error(GaLiExceptionEnum.UPDATE_ERROR);
    }

    /**
     * 分页查询购物车全部商品
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/cart/findAll")
    public ResultInfo findAll(@RequestParam("pageNumber") Integer pageNumber,@RequestParam("pageSize") Integer pageSize){
        PageInfo all = cartService.findAll(pageNumber, pageSize);
        if (all!=null){
            return ResultInfo.success(all);
        }
        return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
    }

    /**
     * 通过id查询购物车中的商品
     * @param selectCartReq
     * @return
     */
    @GetMapping("/cart/findById")
    public ResultInfo findById(@RequestBody @Valid SelectCartReq selectCartReq){
        Cart cart = new Cart();
        BeanUtils.copyProperties(selectCartReq,cart);
        Cart byId = cartService.findById(cart.getId());
        if (byId!=null){
            return ResultInfo.success(byId);
        }
        return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
    }

    /**
     * 选/不选
     * @param
     * @param selected
     * @return
     */
    @PostMapping("/cart/updateSelected")
    public ResultInfo updateSelected(@RequestParam("productId") Integer productId, @RequestParam("selected") Integer selected){
        List<CartVO> updates = cartService.updates(UserFilter.currentUser.getId(), productId, selected);
        return ResultInfo.success(updates);
    }

    /**
     * 全选/全不选
     * @param selected
     * @return
     */
    @PostMapping("/cart/updates")
    public ResultInfo updatesSelected(@RequestParam("selected") Integer selected){
        List<CartVO> updates = cartService.updates(UserFilter.currentUser.getId(), null, selected);
        return ResultInfo.success(updates);
    }
}
